package com.beiben.system.group.service.impl;

import java.util.List;

import com.beiben.system.group.domain.UserGroup;
import com.beiben.system.group.mapper.UserGroupMapper;
import com.ruoyi.common.exception.BaseException;
import com.ruoyi.common.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.beiben.system.group.mapper.GroupMapper;
import com.beiben.system.group.domain.Group;
import com.beiben.system.group.service.IGroupService;

/**
 * 组别Service业务层处理
 * 
 * @author 熊国强
 * @date 2020-09-09
 */
@Service
public class GroupServiceImpl implements IGroupService 
{
    @Autowired
    private GroupMapper groupMapper;

    @Autowired
    private UserGroupMapper userGroupMapper;

    /**
     * 查询组别
     * 
     * @param id 组别ID
     * @return 组别
     */
    @Override
    public Group selectGroupById(Long id)
    {
        return groupMapper.selectGroupById(id);
    }

    /**
     * 查询组别列表
     * 
     * @param group 组别
     * @return 组别
     */
    @Override
    public List<Group> selectGroupList(Group group)
    {
        return groupMapper.selectGroupList(group);
    }
    /**
     * 查询组别列表
     *
     * @param
     * @return 组别集合
     */
    @Override
    public List<Group> selectGroupAll() {
        return groupMapper.selectGroupAll();
    }
    /**
     * 更具用户查询组别列表
     *
     * @param
     * @return 组别集合
     */
    @Override
    public List<Integer> selectUserGroup(Long userId) {
        return groupMapper.selectUserGroup(userId);
    }

    /**
     * 新增组别
     * 
     * @param group 组别
     * @return 结果
     */
    @Override
    public int insertGroup(Group group)
    {
        group.setCreateTime(DateUtils.getNowDate());
        return groupMapper.insertGroup(group);
    }

    /**
     * 修改组别
     * 
     * @param group 组别
     * @return 结果
     */
    @Override
    public int updateGroup(Group group)
    {
        return groupMapper.updateGroup(group);
    }

    /**
     * 批量删除组别
     * 
     * @param ids 需要删除的组别ID
     * @return 结果
     */
    @Override
    public int deleteGroupByIds(Long[] ids)
    {
        List<UserGroup> ugList = userGroupMapper.selectUserGroupByGroupIds(ids);
        if(null!= ugList&& ugList.size()<=0){
            throw new BaseException("改组别已经被关联，禁止删除。");
        }else{
            return groupMapper.deleteGroupByIds(ids);
        }
    }

    /**
     * 删除组别信息
     * 
     * @param id 组别ID
     * @return 结果
     */
    @Override
    public int deleteGroupById(Long id)
    {
        UserGroup userGroup = new UserGroup();
        userGroup.setGroupId(id);
        List<UserGroup> ugList = userGroupMapper.selectUserGroupList(userGroup);
        if(null!= ugList&& ugList.size()<=0){
            throw new BaseException("改组别已经被关联，禁止删除。");
        }else {
            return groupMapper.deleteGroupById(id);
        }
    }
}
